<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "yyh_department".
 *
 * @property int $id
 * @property string $name 名称
 * @property string|null $disease_ids 绑定病种
 * @property string|null $doctor_ids 绑定医生
 * @property int|null $sort 排序
 * @property string $status 状态
 * @property string $createtime 创建时间
 * @property string $lastupdatetime 最后修改时间
 */
class Department extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'yyh_department';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name', 'status', 'createtime', 'lastupdatetime'], 'required'],
            [['disease_ids', 'doctor_ids', 'status'], 'string'],
            [['sort'], 'integer'],
            [['createtime', 'lastupdatetime'], 'safe'],
            [['name'], 'string', 'max' => 32],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '名称',
            'disease_ids' => '绑定病种',
            'doctor_ids' => '绑定医生',
            'sort' => '排序',
            'status' => '状态',
            'createtime' => '创建时间',
            'lastupdatetime' => '最后修改时间',
        ];
    }

    public static function getOptions()
    {
        $res = [];
        $rs = Department::find()->all();
        if (null != $rs && count($rs) > 0) {
            foreach ($rs as $d) {
                $res[$d->id] = $d->name;
            }
        }

        return $res;
    }

    //得到树形结构的病种数据
    public static function getTreeData($where = '', $checked_ids_string = '')
    {
        $department_array = Department::find()->where($where)->orderBy('sort, id')->asArray()->all();
        if ($checked_ids_string != '') {
            $id_array = explode(",", $checked_ids_string);
            if (null != $id_array && count($id_array) > 0) {
                foreach ($id_array as $value_id) {
                    foreach ($department_array as $i => $d) {
                        if ((int)$d['id'] == (int)trim($value_id)) {
                            $department_array[$i]['checked'] = true;
                        }
                    }
                }
            }
        }

        return $department_array;
    }

    //验证医生是否属于科室
    public static function validate_doctor($department_id, $doctor_id_or_ids)
    {
        $department = Department::find()->where("id = {$department_id}")->one();
        if (null != $department) {
            $doctor_array = explode(',', $department->doctor_ids);
            if(is_array($doctor_id_or_ids)){
                if(empty(array_diff($doctor_id_or_ids, $doctor_array))){
                    return true;
                }
            } else if(is_string($doctor_id_or_ids) || is_int($doctor_id_or_ids)){
                if(in_array($doctor_id_or_ids, $doctor_array)){
                    return true;
                }
            }
        }
        return false;
    }

    //验证病种是否属于科室
    public static function validate_disease($department_id, $disease_id_or_ids)
    {
        $department = Department::find()->where("id = {$department_id}")->one();
        if (null != $department) {
            $disease_array = explode(',', $department->disease_ids);
            if(is_array($disease_id_or_ids)){
                if(empty(array_diff($disease_id_or_ids, $disease_array))){
                    return true;
                }
            } else if(is_string($disease_id_or_ids)){
                if(in_array($disease_id_or_ids, $disease_array)){
                    return true;
                }
            }
        }
        return false;
    }
}
